﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace CloudTaobao
{
    public partial class FilterSettings : PhoneApplicationPage
    {
        private App app = Application.Current as App;

        public FilterSettings()
        {
            InitializeComponent();
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            //order by
            if (app.filterParams["order_by"].Equals("popularity:desc"))
                orderBy.SelectedIndex = 0;
            else if (app.filterParams["order_by"].Equals("price:asc"))
                orderBy.SelectedIndex = 1;
            else if (app.filterParams["order_by"].Equals("seller_credit:desc"))
                orderBy.SelectedIndex = 2;
            else if (app.filterParams["order_by"].Equals("volume:desc"))
                orderBy.SelectedIndex = 3;
            else if (app.filterParams["order_by"].Equals("delist_time:desc"))
                orderBy.SelectedIndex = 4;

            //is prepay
            if (app.filterParams["is_prepay"].Equals("true"))
                isPrepay.SelectedIndex = 0;
            else if (app.filterParams["is_prepay"].Equals("no"))
                isPrepay.SelectedIndex = 1;

            //is cod
            if (app.filterParams["is_cod"].Equals("true"))
                isCod.SelectedIndex = 0;
            else if (app.filterParams["is_cod"].Equals("no"))
                isCod.SelectedIndex = 1;

            //promoted_service
            if (app.filterParams["promoted_service"].Equals("2"))
                promotedService.SelectedIndex = 0;
            else if (app.filterParams["promoted_service"].Equals("4"))
                promotedService.SelectedIndex = 1;
            else if (app.filterParams["promoted_service"].Equals("no"))
                promotedService.SelectedIndex = 2;

            //stuff_status
            if (app.filterParams["stuff_status"].Equals("new"))
                stuffStatus.SelectedIndex = 0;
            else if (app.filterParams["stuff_status"].Equals("second"))
                stuffStatus.SelectedIndex = 1;
            else if (app.filterParams["stuff_status"].Equals("unused "))
                stuffStatus.SelectedIndex = 2;
            else if (app.filterParams["stuff_status"].Equals("no"))
                stuffStatus.SelectedIndex = 3;

            //start_price
            if (app.filterParams["start_price"].Equals("no"))
                startPrice.Text = "";
            else
                startPrice.Text = app.filterParams["start_price"];

            //end_price
            if (app.filterParams["end_price"].Equals("no"))
                endPrice.Text = "";
            else
                endPrice.Text = app.filterParams["end_price"];
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            switch (orderBy.SelectedIndex)
            {
                case 0:
                    app.filterParams["order_by"] = "popularity:desc";
                    break;
                case 1:
                    app.filterParams["order_by"] = "price:asc";
                    break;
                case 2:
                    app.filterParams["order_by"] = "seller_credit:desc";
                    break;
                case 3:
                    app.filterParams["order_by"] = "volume:desc";
                    break;
                case 4:
                    app.filterParams["order_by"] = "delist_time:desc";
                    break;
            }

            switch (isPrepay.SelectedIndex)
            {
                case 0:
                    app.filterParams["is_prepay"] = "true";
                    break;
                case 1:
                    app.filterParams["is_prepay"] = "no";
                    break;
            }

            switch (isCod.SelectedIndex)
            {
                case 0:
                    app.filterParams["is_cod"] = "true";
                    break;
                case 1:
                    app.filterParams["is_cod"] = "no";
                    break;
            }

            switch (promotedService.SelectedIndex)
            {
                case 0:
                    app.filterParams["promoted_service"] = "2";
                    break;
                case 1:
                    app.filterParams["promoted_service"] = "4";
                    break;
                case 2:
                    app.filterParams["promoted_service"] = "no";
                    break;
            }

            switch (stuffStatus.SelectedIndex)
            {
                case 0:
                    app.filterParams["stuff_status"] = "new";
                    break;
                case 1:
                    app.filterParams["stuff_status"] = "second";
                    break;
                case 2:
                    app.filterParams["stuff_status"] = "unused";
                    break;
                case 3:
                    app.filterParams["stuff_status"] = "no";
                    break;
            }

            if (startPrice.Text == "")
                app.filterParams["start_price"] = "no";
            else
                app.filterParams["start_price"] = startPrice.Text;

            if (endPrice.Text == "")
                app.filterParams["end_price"] = "no";
            else
                app.filterParams["end_price"] = endPrice.Text;

            NavigationService.GoBack();
        }
    }
}